<?php

// +---------------------------------------------+
// |     Copyright  2010 - 2018 InterPhoto       |
// |     http://www.weentech.com                 |
// |     This file may not be redistributed.     |
// +---------------------------------------------+

include('includes/Admin.Core.php');

CheckAccess('managelanguage');

PrintHeader('语言管理');

$action = ForceIncomingString('action', 'displaylanguages');
$folderpath = BASEPATH.'languages/';

// ############################# GET LANGUAGE DETAILS#############################

function DisplayFileDetails($file, $tdstyle = '')
{
	global $folderpath;

	$filepath = $folderpath . $file;

	echo '<table width="100%" border="0" cellpadding="0" cellspacing="0">
	<tr>
	<td width="10" valign="top" style="padding-right: 15px;">
	<a href="admin.languages.php?action=editfile&filename=' . $file . '"><img border="0" src="'.TEMPLATEPATH.'images/editablefile' . Iif($tdstyle == 'tdrow3', '1' , '2') . '.gif" /></a>
	</td>
	<td valign="top">
	<b>' . $file . '</b> (' .DisplayFilesize(filesize($filepath)). ')<br /><br />
	<a href="admin.languages.php?action=deletefile&filename=' . $file . '">删除文件</a><br /><br />
	<a href="admin.languages.php?action=editfile&filename=' . $file . '">编辑文件</a>
	</td>
	</tr>
	</table>';
}



// ############################### UPLOAD FILE ################################

function UploadFile()
{
	global $folderpath;

	$languagefile     = $_FILES['languagefile'];

	if($languagefile['size'] == 0)	{
		$errors[] = '请选择要上传的文件!';
	}elseif (!IsUploadedFile($languagefile['tmp_name']) || !($languagefile['tmp_name'] != 'none' && $languagefile['tmp_name'] && $languagefile['name']) ){
		$errors[] ='Possible file upload attack!';
	}else if(strtolower(substr($languagefile['name'], -3)) != 'php') {
		$errors[] = '不允许上传的文件类型!';
	}else if (file_exists($folderpath . $languagefile['name'])) {
		$errors[] = '目标文件夹内存在同名的文件, 请先删除原文件再上传!';
	}else{
		@chmod($folderpath, 0777);

		if((function_exists('move_uploaded_file') AND @move_uploaded_file($languagefile['tmp_name'], $folderpath . $languagefile['name'])) OR @copy($languagefile['tmp_name'], $folderpath . $languagefile['name']))	{
			@chmod($folderpath . $languagefile['name'], 0777);
			@unlink($languagefile['tmp_name']);
		}else{
			$errors[] = '文件夹 "' . BASEURL . 'languages/" 不可写!<br />注: 将文件夹 "' . BASEURL . 'languages/" 的属性改为: 777';
		}
	}

	if(isset($errors))
	{
		PrintErrors($errors, '上传文件错误');
		DisplayLanguages();
	}else{
		DisplayLanguages(true);
	}
}

// ############################### DELETE FILE ################################

function DeleteFile()
{
	global $folderpath;
	$filename = ForceIncomingString('filename');

	if(IsPost('deletefile')){
		if(@unlink($folderpath.$filename))
		{
			PrintRedirect('admin.languages.php', 1);
		}else{
			$errors[] = '无法删除语言文件!';
			PrintErrors($errors, '删除失败');
		}
	}else if(IsPost('cancel')){
		PrintRedirect('admin.languages.php');
	}


	PrintSection('删除文件');
	echo '<table width="100%" border="0" cellpadding="5" cellspacing="0">
		<tr><td class="tdrow1" colspan="2">确认删除</td></tr>
		<tr>
		<td class="tdrow2" width="70%" valign="top">语言文件: <b>' . $filename . '</b> (' .DisplayFilesize(filesize($folderpath.$filename)) . ')<br /><br />确定删除此文件吗?
		</td>
		<td class="tdrow3" valign="top">
		<form method="post" action="admin.languages.php">
		<input type="hidden" name="action" value="deletefile" />
		<input type="hidden" name="filename" value="' . $filename . '" />
		<input type="submit" name="deletefile" value=" 确定删除 " />&nbsp;&nbsp;
		<input type="submit" name="cancel" value=" 取消 " />
		</form>
		</td>
		</tr>
		</table>';
	EndSection();
}


// ############################### EDIT FILE ################################

function EditFile()
{
	global $folderpath;

	$filename = ForceIncomingString('filename');

	if(IsPost('savefile')){

		if (is_writable($folderpath.$filename)) {
			$filecontent = trim($_POST['filecontent']);
			if (get_magic_quotes_gpc()) {
				$filecontent = stripslashes($filecontent);
			}

			$fd = fopen($folderpath.$filename, 'wb');
			fputs($fd,$filecontent);
			PrintRedirect('admin.languages.php', 1);
		}else{
			$errors = '语言文件('.$filename.')不可写! 请将其属性设置为: 777';
			PrintErrors($errors, '编辑语言错误');
		}

	}else if(IsPost('cancel')){
		PrintRedirect('admin.languages.php');
	}

	if ($filename){ $oldfilecontent = implode("",file($folderpath . $filename));}

	PrintSection('编辑语言文件');
	echo '<form method="post" action="admin.languages.php">
	<table width="100%" border="0" cellpadding="5" cellspacing="0">
	<tr>
	<td class="tdrow1">
	<input type="hidden" name="action" value="editfile" />
	<input type="hidden" name="filename" value="' . $filename . '" />
	<input type="submit" name="savefile" value=" 保存更新 " onclick="return confirm(\'确定更新文件吗?\');" />&nbsp;&nbsp;
	<input type="submit" name="cancel" value=" 取消 " /></td>
	</tr>
	<tr>
	<td class="tdrow2" width="100%" valign="top">当前文件: ' . BASEURL . 'languages/'.$filename.'<BR><BR>
	<textarea rows="36" style="width:100%;" name="filecontent" >' . $oldfilecontent . '</textarea>
	</td>
	</tr>
	<tr>
	<td class="tdrow1">
	<input type="submit" name="savefile" value=" 保存更新 " onclick="return confirm(\'确定更新文件吗?\');" />&nbsp;&nbsp;
	<input type="submit" name="cancel" value=" 取消 " /></td>
	</tr>
	</table></form>';

	EndSection();

}


// ############################### DISPLAY LANGUAGES ##############################

function DisplayLanguages($uploaded = '')
{
	global $folderpath;

	PrintSection('上传语言文件');
	echo '<table width="100%" border="0" cellpadding="5" cellspacing="0">
		<tr>
		<td class="tdrow1"><b>当前文件夹:</b> ' . BASEURL . 'languages/</td>
		</tr>
		<tr>
		<td class="tdrow2">';
	  
	if($uploaded){
		echo '<font class=ohblueb>语言文件上传成功!</font><br /><br />';
	}

	echo '上传文件到当前文件夹:<br />
		<form enctype="multipart/form-data" method="post" action="admin.languages.php" name="upload_form">
		<input type="hidden" name="action" value="uploadfile" />
		<input name="languagefile" type="file" size="38" />
		<input type="submit" value="上传文件" />
		</form><br />(注: 仅可上传php文件, 可复制某语言文件, 翻译后重命名上传)</td>
		</tr>
		</table>';

	EndSection();

	PrintSection('语言文件列表');

	$files   = GetLangs(1);
	$columncount = 0;
	$tdstyle = 'tdrow3';
	echo '<table width="100%" border="0" cellpadding="5" cellspacing="0"><tr>
		<td class="tdrow1" colspan="3"><b>当前文件夹:</b> ' . BASEURL . 'languages/</td>
		</tr>';

	for($i = 0; $i < count($files); $i++) {
		$columncount++;

		if($columncount == 1)	{
			echo '<tr>';
			$tdstyle = ($tdstyle == 'tdrow3') ? 'tdrow2' : 'tdrow3';
		}

		echo '<td width="33%" class="' . $tdstyle . '">';
		DisplayFileDetails($files[$i], $tdstyle);
		echo '</td>';

		if($columncount == 3)
		{
			echo '</tr>';
			$columncount = 0;
		}
	}

	@closedir($handle);

	if($columncount != 0 && $columncount != 3)
	{
		while($columncount < 3)
		{
			$columncount++;
			echo '<td class="' . $tdstyle . '">&nbsp;</td>';
		}
		echo '</tr>';
	}

	echo '</table>';

	EndSection();
}



// ############################## SELECT FUNCTION ##############################


switch($action)
{
  case 'uploadfile':
    UploadFile();
  break;

  case 'deletefile':
    DeleteFile();
  break;
  
  case 'editfile':
    EditFile();
  break;

  case 'displaylanguages':
    DisplayLanguages();
  break;
}


PrintFooter();

?>